/**
 * Created by XianTao on 2015/1/8
 *
 * Copyright (c) 2015-2015
 * Apache License
 * Version 2.0, January 2004
 * http://www.apache.org/licenses/
 */
package com.taotao.wxcs;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultText;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 无线城市--公共事业缴费
 */
@Controller
@RequestMapping(value = "/wxcs/public")
public class PublicUtilitiesPayment {
    public static final String WRONG_PARAM_MESSAGE = "请先关注内蒙古移动微信号，并正确填写所有的字段";
    private static Logger logger = Logger.getLogger(PublicUtilitiesPayment.class);

    private static final String BUSI_TYPE_S = "S";  //水
    private static final String BUSI_TYPE_D = "D";  //电
    private static final String BUSI_TYPE_Q = "Q";  //燃气
    private static final String BUSI_TYPE_T = "T";  //数字电视

    private static final List<Company> companyList = new ArrayList<Company>();
    static {
        companyList.add(new Company("0470", BUSI_TYPE_D, "NMELEC002", "内蒙古东部电力有限公司呼伦贝尔电业局"));
        companyList.add(new Company("0470", BUSI_TYPE_D, "NMELEC019", "呼伦贝尔额尔古纳宏源电力公司"));
        companyList.add(new Company("0470", BUSI_TYPE_D, "NMELEC021", "国网内蒙古东部阿荣旗供电有限公司"));
        companyList.add(new Company("0470", BUSI_TYPE_D, "NMELEC018", "呼伦贝尔鄂伦春春晖电力有限责任公司"));
        companyList.add(new Company("0470", BUSI_TYPE_D, "NMELEC033", "国网内蒙古东部鄂温克族自治旗供电有限公司"));
        companyList.add(new Company("0470", BUSI_TYPE_D, "NMELEC032", "陈巴尔虎旗电力有限责任公司"));
        companyList.add(new Company("0470", BUSI_TYPE_D, "NMELEC023", "国网内蒙古东部新巴尔虎左旗供电有限公司"));
        companyList.add(new Company("0470", BUSI_TYPE_D, "NMELEC015", "国网内蒙古东部莫力达瓦达斡尔族自治旗供电有限公司"));
        companyList.add(new Company("0470", BUSI_TYPE_D, "NMELEC034", "国网内蒙古东部新巴尔虎右旗供电有限公司"));

        companyList.add(new Company("0471", BUSI_TYPE_Q, "NMGAS001", "呼和浩特中燃城市燃气发展有限公司"));
        companyList.add(new Company("0471", BUSI_TYPE_D, "NMELEC601", "内蒙古电力(集团)有限责任公司呼和浩特供电局"));

        companyList.add(new Company("0472", BUSI_TYPE_D, "NMELEC602", "内蒙古电力(集团)有限责任公司包头供电局"));

        companyList.add(new Company("0473", BUSI_TYPE_D, "NMELEC603", "内蒙古电力(集团)有限责任公司乌海电业局"));

        companyList.add(new Company("0474", BUSI_TYPE_D, "NMELEC605", "内蒙古电力(集团)有限责任公司乌兰察布电业局"));

        companyList.add(new Company("0475", BUSI_TYPE_D, "NMELEC003", "国网内蒙古东部电力有限公司通辽供电公司"));
        companyList.add(new Company("0475", BUSI_TYPE_D, "NMELEC029", "国网内蒙古东部科左中旗供电有限公司"));
        companyList.add(new Company("0475", BUSI_TYPE_D, "NMELEC030", "国网内蒙古东部科左后旗供电有限公司"));
        companyList.add(new Company("0475", BUSI_TYPE_D, "NMELEC027", "国网内蒙古东部开鲁县供电有限公司"));
        companyList.add(new Company("0475", BUSI_TYPE_D, "NMELEC026", "通辽库伦旗农电公司"));
        companyList.add(new Company("0475", BUSI_TYPE_D, "NMELEC028", "国网内蒙古东部奈曼旗供电有限公司"));
        companyList.add(new Company("0475", BUSI_TYPE_D, "NMELEC031", "通辽扎鲁特旗农电公司"));

        companyList.add(new Company("0476", BUSI_TYPE_D, "NMELEC001", "国网内蒙古东部电力有限公司赤峰供电公司"));
        companyList.add(new Company("0476", BUSI_TYPE_D, "NMELEC017", "赤峰巴林左旗农电局"));
        companyList.add(new Company("0476", BUSI_TYPE_D, "NMELEC005", "赤峰巴林右旗农电局"));
        companyList.add(new Company("0476", BUSI_TYPE_D, "NMELEC006", "赤峰克什克腾旗农电局"));
        companyList.add(new Company("0476", BUSI_TYPE_D, "NMELEC008", "赤峰林西县农电局"));
        companyList.add(new Company("0476", BUSI_TYPE_D, "NMELEC009", "赤峰宁城农电局"));
        companyList.add(new Company("0476", BUSI_TYPE_D, "NMELEC010", "赤峰元宝山区农电局"));
        companyList.add(new Company("0476", BUSI_TYPE_D, "NMELEC011", "赤峰翁牛特旗农电局"));
        companyList.add(new Company("0476", BUSI_TYPE_D, "NMELEC012", "赤峰阿鲁科尔沁旗农电局"));
        companyList.add(new Company("0476", BUSI_TYPE_D, "NMELEC014", "赤峰敖汉旗农电局"));
        companyList.add(new Company("0476", BUSI_TYPE_D, "NMELEC013", "赤峰松山区农电局"));
        companyList.add(new Company("0476", BUSI_TYPE_D, "NMELEC016", "赤峰喀喇沁农电局"));
        companyList.add(new Company("0476", BUSI_TYPE_S, "NMWAT001", "赤峰市自来水总公司"));

        companyList.add(new Company("0477", BUSI_TYPE_D, "NMELEC604", "内蒙古电力(集团)有限责任公司鄂尔多斯电业局"));
        companyList.add(new Company("0477", BUSI_TYPE_D, "NMELEC607", "内蒙古电力(集团)有限责任公司薛家湾电业局"));
        companyList.add(new Company("0477", BUSI_TYPE_D, "NMELEC007", "内蒙古鄂尔多斯鄂托克旗农电局"));

        companyList.add(new Company("0478", BUSI_TYPE_D, "NMELEC606", "内蒙古电力(集团)有限责任公司巴彦淖尔电业局"));

        companyList.add(new Company("0479", BUSI_TYPE_D, "NMELEC608", "内蒙古电力(集团)有限责任公司锡林郭勒电业局"));
        companyList.add(new Company("0479", BUSI_TYPE_D, "NMELEC698", "二连浩特市供电局"));
        companyList.add(new Company("0479", BUSI_TYPE_D, "NMELEC699", "苏尼特右旗供电局"));

        companyList.add(new Company("0482", BUSI_TYPE_D, "NMELEC004", "国网内蒙古东部电力有限公司兴安供电公司"));
        companyList.add(new Company("0482", BUSI_TYPE_D, "NMELEC024", "国网内蒙古东部科右前旗供电有限公司"));
        companyList.add(new Company("0482", BUSI_TYPE_D, "NMELEC025", "国网内蒙古东部科右中旗供电有限公司"));
        companyList.add(new Company("0482", BUSI_TYPE_D, "NMELEC020", "国网内蒙古东部扎赉特旗供电有限公司"));
        companyList.add(new Company("0482", BUSI_TYPE_D, "NMELEC022", "兴安盟突泉县电力有限责任公司"));

        companyList.add(new Company("0483", BUSI_TYPE_D, "NMELEC611", "内蒙古电力(集团)有限责任公司阿拉善电业局"));

        companyList.add(new Company("全区", BUSI_TYPE_T, "NMDVB001", "内蒙古广播电视网络集团有限公司"));
    }

    @RequestMapping(value = "/area/{type}", method = RequestMethod.POST)
    @ResponseBody
    public Object getArea(@PathVariable String type) {
        if (BUSI_TYPE_S.equalsIgnoreCase(type)) {
            return "[{\"id\":\"0476\", \"text\":\"赤峰\"}, " +
                    "{\"id\":\"0\", \"text\":\"其他盟市暂不支持\"}]";
        }

        if (BUSI_TYPE_Q.equalsIgnoreCase(type)) {
            return "[{\"id\":\"0471\", \"text\":\"呼和浩特\"}, " +
                    "{\"id\":\"0\", \"text\":\"其他盟市暂不支持\"}]";
        }

        return "[{\"id\":\"0470\", \"text\":\"呼伦贝尔\"}, " +
                "{\"id\":\"0471\", \"text\":\"呼和浩特\"}, " +
                "{\"id\":\"0472\", \"text\":\"包头\"}, " +
                "{\"id\":\"0473\", \"text\":\"乌海\"}, " +
                "{\"id\":\"0474\", \"text\":\"乌兰察布\"}, " +
                "{\"id\":\"0475\", \"text\":\"通辽\"}, " +
                "{\"id\":\"0476\", \"text\":\"赤峰\"}, " +
                "{\"id\":\"0477\", \"text\":\"鄂尔多斯\"}, " +
                "{\"id\":\"0478\", \"text\":\"巴彦淖尔\"}, " +
                "{\"id\":\"0479\", \"text\":\"锡林郭勒\"}, " +
                "{\"id\":\"0482\", \"text\":\"兴安\"}, " +
                "{\"id\":\"0483\", \"text\":\"阿拉善\"}]";
    }

    @RequestMapping(value = "/util/{area}/{type}", method = RequestMethod.POST)
    @ResponseBody
    public Object getUtil(@PathVariable String area, @PathVariable String type) {
        List<Company> selectCompanyList = new ArrayList<Company>();
        for (Company company : companyList) {
            if ((company.getArea().equals(area) || company.getArea().equals("全区")) && company.getType().equals(type)) {
                selectCompanyList.add(company);
            }
        }
        return selectCompanyList;
    }

    @RequestMapping(value = "/userQuery", method = RequestMethod.POST)
    @ResponseBody
    public Object userQuery(HttpServletRequest request) {
        String type = request.getParameter("type");
        String area = request.getParameter("area");
        String util = request.getParameter("util");
        String consNo = request.getParameter("consNo");
        String userId = request.getParameter("userId");
        String productId = request.getParameter("tvPackage");
        String month = request.getParameter("month");
        //TODO: 验证参数的合法性
        if (StringUtils.isBlank(type) || StringUtils.isBlank(area) || StringUtils.isBlank(util) || StringUtils.isBlank(consNo) || StringUtils.isBlank(userId)
                ||(!BUSI_TYPE_S.equalsIgnoreCase(type) && !BUSI_TYPE_D.equalsIgnoreCase(type) && !BUSI_TYPE_Q.equalsIgnoreCase(type) && !BUSI_TYPE_T.equalsIgnoreCase(type))
                || (BUSI_TYPE_T.equalsIgnoreCase(type) && (StringUtils.isBlank(productId) || StringUtils.isBlank(month) || !StringUtils.isNumeric(month)))
                ) {
            return getErrorResponse(WRONG_PARAM_MESSAGE);
        }

        String xmlInfo = getUserQueryXml(type, area, util, consNo, userId, productId, month);

        return getDataFromUrl(xmlInfo);
    }

    @RequestMapping(value = "/userCharge", method = RequestMethod.POST)
    @ResponseBody
    public Object userCharge(HttpServletRequest request) {
        String type = request.getParameter("type");
        String userId = request.getParameter("userId");
        String contNo = request.getParameter("contNo");
        String amtSum = request.getParameter("payMoney");
        String productId = request.getParameter("tvPackage");
        String month = request.getParameter("month");
        String util = request.getParameter("util");
        //TODO: 验证参数的合法性
        if (StringUtils.isBlank(type) || StringUtils.isBlank(userId) || StringUtils.isBlank(contNo) || StringUtils.isBlank(amtSum) || StringUtils.isBlank(util)
                ||(!BUSI_TYPE_S.equalsIgnoreCase(type) && !BUSI_TYPE_D.equalsIgnoreCase(type) && !BUSI_TYPE_Q.equalsIgnoreCase(type) && !BUSI_TYPE_T.equalsIgnoreCase(type))
                || (BUSI_TYPE_T.equalsIgnoreCase(type) && (StringUtils.isBlank(productId) || StringUtils.isBlank(month) || !StringUtils.isNumeric(month)))
                ) {
            return getErrorResponse(WRONG_PARAM_MESSAGE);
        }

        String xmlInfo = getUserChargeXml(type, userId, contNo, amtSum, productId, month, util);

        return getDataFromUrl(xmlInfo);
    }

    @RequestMapping(value = "/userTransaction", method = RequestMethod.POST)
    @ResponseBody
    public Object userTransaction(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        String timeStamp = request.getParameter("timeStamp");
        String timeStampEnd = request.getParameter("timeStampEnd");
        //TODO: 验证参数的合法性
        if (StringUtils.isBlank(userId)) {
            return getErrorResponse(WRONG_PARAM_MESSAGE);
        }

        long now = System.currentTimeMillis();
        Calendar nowCal = Calendar.getInstance();
        nowCal.setTimeInMillis(now);
        nowCal.add(Calendar.MONTH, -1);
        timeStamp = getTimeStampFormSimpleDate(timeStamp, nowCal.getTimeInMillis());
        timeStampEnd = getTimeStampFormSimpleDate(timeStampEnd, now + 24 * 3600 * 1000L);

        String xmlInfo = getUserTransactionXml(userId, timeStamp, timeStampEnd);

        return getDataFromUrl(xmlInfo);
    }

    private String getDataFromUrl(String postData) {
        StringBuilder sb = new StringBuilder();
        String urlStr = "http://218.202.134.21:8899/WL_CITY/control";
        try {
            URL url = new URL(urlStr);
            URLConnection con = url.openConnection();
            con.setDoOutput(true);
            con.setRequestProperty("Pragma:", "no-cache");
            con.setRequestProperty("Cache-Control", "no-cache");
            con.setRequestProperty("Content-Type", "text/xml");

            OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
            out.write(new String(postData.getBytes("GBK")));
            out.flush();
            out.close();
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "GBK"));
            for (String line = br.readLine(); line != null; line = br.readLine()) {
                sb.append(line);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        logger.error("urlStr=" + urlStr);
        logger.error("xmlInfo=" + postData);
        logger.error("result=" + sb.toString());
        if (sb.length() == 0) {
            sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<NMSHY><ERR_NO>999</ERR_NO></NMSHY>");
        }
        return parseXml2Json(sb.toString());
    }

    private String getUserQueryXml(String busiType, String area, String util, String consNo, String userId, String productId, String month) {
        String nowStr = getTimeStamp(new Date());
        String randomStr = "" + ((int)(Math.random() * (9999-1000)) + 1000);

        StringBuilder sb = new StringBuilder();
        sb.append("<?xml version=\"1.0\" encoding=\"GBK\"?>");
        sb.append("<NMSHY>");
        sb.append("<FUN_CODE>USER_QUERY</FUN_CODE>");       //功能
        sb.append("<BUSI_TYPE>").append(busiType).append("</BUSI_TYPE>");  //缴费类型
        sb.append("<AREA_CODE>").append(area).append("</AREA_CODE>");   //盟市
        sb.append("<UTIL_ID>").append(util).append("</UTIL_ID>");   //缴费单位
        sb.append("<CONS_NO>").append(consNo).append("</CONS_NO>"); //户号
        sb.append("<USER_ID>").append(userId).append("</USER_ID>"); //用户唯一标示：appId;openId
        sb.append("<CHANNAL_NO>SYS005</CHANNAL_NO>");   //渠道号：微信营业厅
        sb.append("<TIME_STAMP>").append(nowStr).append("</TIME_STAMP>");
        sb.append("<SERIAL_NO>").append(nowStr).append(randomStr).append("</SERIAL_NO>");
        sb.append("<PLATFORM_ID></PLATFORM_ID>");
        sb.append("<MOBILE_NO></MOBILE_NO>");
        sb.append("<MIMA></MIMA>");
        sb.append("<MAC>").append(DigestUtils.md5Hex(nowStr + randomStr + nowStr + "SYS005" + userId)).append("</MAC>");
        if (BUSI_TYPE_T.equals(busiType)) {
            sb.append("<PRODUCT_ID>").append(productId).append("</PRODUCT_ID>");
            sb.append("<MONTH>").append(month).append("</MONTH>");
        }
        sb.append("</NMSHY>");
        return sb.toString();
    }

    private String getUserChargeXml(String busiType, String userId, String contNo, String amtSum, String productId, String month, String util) {
        String nowStr = getTimeStamp(new Date());
        String randomStr = "" + ((int)(Math.random() * (9999-1000)) + 1000);

        StringBuilder sb = new StringBuilder();
        sb.append("<?xml version=\"1.0\" encoding=\"GBK\"?>");
        sb.append("<NMSHY>");
        sb.append("<FUN_CODE>USER_CHARGE</FUN_CODE>");       //功能
        sb.append("<USER_ID>").append(userId).append("</USER_ID>"); //用户唯一标示：appId;openId
        sb.append("<CONT_NO>").append(contNo).append("</CONT_NO>"); //订单号
        sb.append("<AMT_SUM>").append(amtSum).append("</AMT_SUM>"); //缴费金额
        sb.append("<CHANNAL_NO>SYS005</CHANNAL_NO>");   //渠道号：微信营业厅
        sb.append("<TIME_STAMP>").append(nowStr).append("</TIME_STAMP>");
        sb.append("<SERIAL_NO>").append(nowStr).append(randomStr).append("</SERIAL_NO>");
        sb.append("<UTIL_ID>").append(util).append("</UTIL_ID>");
        sb.append("<PLATFORM_ID></PLATFORM_ID>");
        sb.append("<MOBILE_NO></MOBILE_NO>");
        sb.append("<MIMA></MIMA>");
        sb.append("<MAC>").append(DigestUtils.md5Hex(nowStr + randomStr + nowStr + "SYS005" + userId)).append("</MAC>");
        if (BUSI_TYPE_T.equals(busiType)) {
            sb.append("<PRODUCT_ID>").append(productId).append("</PRODUCT_ID>");
            sb.append("<MONTH>").append(month).append("</MONTH>");
        }
        sb.append("</NMSHY>");
        return sb.toString();
    }

    private String getUserTransactionXml(String userId, String timeStamp, String timeStampEnd) {
        String randomStr = "" + ((int)(Math.random() * (9999-1000)) + 1000);

        StringBuilder sb = new StringBuilder();
        sb.append("<?xml version=\"1.0\" encoding=\"GBK\"?>");
        sb.append("<NMSHY>");
        sb.append("<FUN_CODE>USER_TRANSACTION</FUN_CODE>");       //功能
        sb.append("<USER_ID>").append(userId).append("</USER_ID>"); //用户唯一标示：appId;openId
        sb.append("<CHANNAL_NO>SYS005</CHANNAL_NO>");   //渠道号：微信营业厅
        sb.append("<TIME_STAMP>").append(timeStamp).append("</TIME_STAMP>");
        sb.append("<TIME_STAMP_END>").append(timeStampEnd).append("</TIME_STAMP_END>");
        sb.append("<SERIAL_NO>").append(timeStamp).append(randomStr).append("</SERIAL_NO>");
        sb.append("<PLATFORM_ID></PLATFORM_ID>");
        sb.append("<MOBILE_NO></MOBILE_NO>");
        sb.append("<MAC>").append(DigestUtils.md5Hex(timeStamp + randomStr + timeStamp + "SYS005" + userId)).append("</MAC>");
        sb.append("</NMSHY>");
        return sb.toString();
    }

    private String getErrorResponse(String message) {
        return "{\"ERR_NO\":\"999\", \"NOTE\":\"" + message + "\"}";
    }

    private static final SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
    private static final SimpleDateFormat simpleDf = new SimpleDateFormat("yyyy-MM-dd");
    private String getTimeStamp(Date date) {
        return df.format(date);
    }
    private String parseXml2Json(String xml) {
        boolean sign = false;
        StringBuilder json = new StringBuilder();
        SAXReader reader = new SAXReader();
        Document document = null;
        try {
            document = reader.read(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        } catch (DocumentException e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        if (document != null) {
            Element root = document.getRootElement();
            List<Element> elementList = root.elements();
            json.append("{");
            for (Element element : elementList) {
                List<Element> contentList = element.content();
                if ("NOTE".equals(element.getName()) && contentList.size() == 21) {
                    List<DefaultText> noteContentList = element.content();
                    StringBuilder text = new StringBuilder();
                    for (DefaultText ele : noteContentList) {
                        text.append(ele.getText());
                    }
                    Element temp = new DOMElement(element.getName());
                    temp.setText(text.toString());
                    element.setText(text.toString());
                    contentList = element.content();
                }
                if (contentList.size() == 1) {
                    sign = false;
                    json.append("\"").append(element.getName()).append("\"");
                    json.append(":");
                    StringBuilder text = new StringBuilder(element.getText());
                    if ("PREPAY_BAL".equals(element.getName())
                            || "DEBT_MONEY".equals(element.getName())) {
                        if (text.length() < 3) {
                            text.insert(0, "000".substring(0, 3 - text.length()));
                        }
                        text.insert(text.length() - 2, ".");
                        text.append("元");
                    }
                    json.append("\"").append(text).append("\"");
                    json.append(",");
                } else {
                    json.append("\"").append(element.getName()).append("\"");
                    json.append(":");
                    json.append("{");
                    for (Element subElement : contentList) {
                        List<Element> subContentList = subElement.content();
                        if (subContentList.size() == 1) {
                            json.append("\"").append(subElement.getName()).append("\"");
                            json.append(":");
                            StringBuilder text = new StringBuilder(subElement.getText());
                            if ("PREPAY_BAL".equals(subElement.getName())
                                    || "DEBT_MONEY".equals(subElement.getName())) {
                                if (text.length() < 3) {
                                    text.insert(0, "000".substring(0, 3 - text.length()));
                                }
                                text.insert(text.length() - 2, ".");
                                text.append("元");
                            }
                            json.append("\"").append(text).append("\"");
                            json.append(",");
                        } else {
                            if (!sign) {
                                json.append("\"").append(subElement.getName()).append("\"");
                                json.append(":");
                                json.append("[");
                                sign = true;
                            }
                            json.append("{");
                            for (Element subSubElement : subContentList) {
                                json.append("\"").append(subSubElement.getName()).append("\"");
                                json.append(":");
                                StringBuilder text = new StringBuilder(subSubElement.getText());
                                if ("PREPAY_BAL".equals(subSubElement.getName())
                                        || "DEBT_MONEY".equals(subSubElement.getName())
                                        || "AMT".equals(subSubElement.getName())) {
                                    if (text.length() < 3) {
                                        text.insert(0, "000".substring(0, 3 - text.length()));
                                    }
                                    text.insert(text.length() - 2, ".");
                                    text.append("元");
                                }
                                if ("UTIL_ID".equals(subSubElement.getName())) {
                                    for (Company company : companyList) {
                                        if (company.getId().equals(subSubElement.getText())) {
                                            text.delete(0, text.length());
                                            text.append(company.getText());
                                        }
                                    }
                                }
                                json.append("\"").append(text).append("\"");
                                json.append(",");
                            }
                            json.deleteCharAt(json.length() - 1);
                            json.append("}");
                            json.append(",");
                        }
                    }
                    json.deleteCharAt(json.length() - 1);
                    json.append("}");
                    json.append(",");
                }
                if (sign) {
                    json.insert(json.length() - 2, "]");
                }
            }
            json.deleteCharAt(json.length() - 1);
            json.append("}");
        }
        return json.toString();
    }
    private String getTimeStampFormSimpleDate(String date, long defaultDate) {
        Date tempDate;
        try {
            tempDate = simpleDf.parse(date);
        } catch (Exception e) {
            tempDate = new Date(defaultDate);
        }
        return getTimeStamp(tempDate);
    }
}
